﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Alserve.Ffl;
using Alserve.Ffl.Data;

public partial class ffl_admin_transfers : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //if (!Security.IsAdmin(Page))
            //    Response.Redirect("~//ffl//anonymous//default.aspx");

            Club thisClub = new Club();

            if (thisClub.LoadAll())
            {
                ClubDropDown.DataSource = thisClub.DefaultView;
                ClubDropDown.DataBind();
            }

            Transfer thisTransfer = new Transfer();
            if (thisTransfer.LoadAll())
            {
                PlayerRepeater.DataSource = thisTransfer.DefaultView;
                PlayerRepeater.DataBind();
            }
        }
    }

    protected void ShowAll_OnCheckedChange(Object Sender, EventArgs e)
    {
        if (ShowAll.Checked)
        {
            ClubDropDown.Enabled = false;
            ClubLabel.Enabled = false;
            Transfer thisTransfer = new Transfer();
            if (thisTransfer.LoadAll())
            {
                PlayerRepeater.DataSource = thisTransfer.DefaultView;
                PlayerRepeater.DataBind();
            }
        }
        else
        {
            ClubDropDown.Enabled = true;
            ClubLabel.Enabled = true;
            Transfer thisTransfer = new Transfer();
            thisTransfer.Where.ClubId.Value = Convert.ToInt32(ClubDropDown.SelectedItem.Value);
            if (thisTransfer.Query.Load())
            {
                PlayerRepeater.DataSource = thisTransfer.DefaultView;
                PlayerRepeater.DataBind();
            }
        }
    }

    protected void PlayerRepeater_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Boolean newPlayer = Convert.ToBoolean(((HiddenField)e.Item.FindControl("NewPlayerHidden")).Value);
            DropDownList positionDropDown = (DropDownList)e.Item.FindControl("PositionDropDown");
            TextBox forenameTextBox = (TextBox)e.Item.FindControl("ForenameTextBox");
            TextBox surnameTextBox = (TextBox)e.Item.FindControl("SurnameTextBox");
            HiddenField clubIdHidden = (HiddenField)e.Item.FindControl("ClubIdHidden");
            Label transferToLabel = (Label)e.Item.FindControl("TransferToLabel");

            Int32 newClubId = Convert.ToInt32(clubIdHidden.Value);

            Position thisPosition = new Position();
            if (thisPosition.LoadAll())
            {
                positionDropDown.DataSource = thisPosition.DefaultView;
                positionDropDown.DataBind();
            }


            Club thisClub = new Club();
            thisClub.Where.Id.Value = newClubId;
            if (thisClub.Query.Load())
            {
                transferToLabel.Text = thisClub.ClubName;
            }

            if (newPlayer)
            {

                positionDropDown.SelectedIndex = Convert.ToInt32(((HiddenField)e.Item.FindControl("PositionHidden")).Value) - 1;

                String playerName = (String)DataBinder.Eval(e.Item.DataItem, "PlayerName");
                Int32 split = playerName.IndexOf(" ");
                
                if (split > -1)
                {
                    forenameTextBox.Text = playerName.Substring(0, split).Trim();
                    surnameTextBox.Text = playerName.Substring(split).Trim();
                }
                else
                {
                    surnameTextBox.Text = playerName.Trim();
                }
            }
            else
            {

                HiddenField existingPlayerIdHidden = (HiddenField)e.Item.FindControl("ExistingPlayerIdHidden");
                Label transferFromLabel = (Label)e.Item.FindControl("TransferFromLabel");
                Int32 existingPlayerId = Convert.ToInt32(existingPlayerIdHidden.Value);

                _PlayerView thisPlayerView = new _PlayerView();
                thisPlayerView.Where.PlayerId.Value = existingPlayerId;
                thisPlayerView.Query.AddResultColumn(_PlayerView.ColumnNames.Forename);
                thisPlayerView.Query.AddResultColumn(_PlayerView.ColumnNames.Surname);
                thisPlayerView.Query.AddResultColumn(_PlayerView.ColumnNames.ClubName);
                
                if (thisPlayerView.Query.Load())
                {
                    forenameTextBox.Text = thisPlayerView.Forename;
                    forenameTextBox.Enabled = false;
                    surnameTextBox.Text = thisPlayerView.Surname;
                    surnameTextBox.Enabled = false;
                    transferFromLabel.Text = thisPlayerView.ClubName;
                }

                positionDropDown.Visible = false;

            }
        }
    }

    protected void ClubDropDown_OnSelectedIndexChanged(object source, EventArgs e)
    {
        Transfer thisTransfer = new Transfer();
        thisTransfer.Where.ClubId.Value = Convert.ToInt32(ClubDropDown.SelectedItem.Value);
        thisTransfer.Query.AddOrderBy(Transfer.ColumnNames.PositionId, MyGeneration.dOOdads.WhereParameter.Dir.ASC);
        thisTransfer.Query.AddOrderBy(Transfer.ColumnNames.PlayerName, MyGeneration.dOOdads.WhereParameter.Dir.ASC);
        if (thisTransfer.Query.Load())
        {
            PlayerRepeater.DataSource = thisTransfer.DefaultView;
            PlayerRepeater.DataBind();
        }
    }

    protected void ImportFromEspn_Click(object sender, EventArgs e)
    {
        Transfer thisTransfer = new Transfer();
        if (thisTransfer.LoadAll())
        {
            thisTransfer.DeleteAll();
            thisTransfer.Save();
        }
        
        EspnImport thisImport = new EspnImport();
        thisImport.ImportTransfers();

        if (thisTransfer.LoadAll())
        {
            PlayerRepeater.DataSource = thisTransfer.DefaultView;
            PlayerRepeater.DataBind();
        }
    }

    protected void ActionAllButton_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem playerItem in PlayerRepeater.Items)
        {

            Boolean newPlayer = Convert.ToBoolean(((HiddenField)playerItem.FindControl("NewPlayerHidden")).Value);
            Int32 transferId = Convert.ToInt32(((HiddenField)playerItem.FindControl("TransferIdHidden")).Value);
            String forename = ((TextBox)playerItem.FindControl("ForenameTextBox")).Text.Trim();
            String surname = ((TextBox)playerItem.FindControl("SurnameTextBox")).Text.Trim();
            Int32 positionId = Convert.ToInt32(((DropDownList)playerItem.FindControl("PositionDropDown")).SelectedValue);
            Int32 clubId = Convert.ToInt32(((HiddenField)playerItem.FindControl("ClubIdHidden")).Value);
            DateTime createDate = DateTime.Now;

            if (newPlayer)
            {

                //Add Player to Player
                Player thisPlayer = new Player();
                thisPlayer.AddNew();
                thisPlayer.Forename = forename;
                thisPlayer.Surname = surname;
                thisPlayer.PositionId = positionId;
                thisPlayer.Save();

                //Add Player to EspnPlayer
                EspnPlayer thisEspnPlayer = new EspnPlayer();
                thisEspnPlayer.AddNew();
                thisEspnPlayer.PlayerId = thisPlayer.Id;
                thisEspnPlayer.EspnPlayerId = Convert.ToInt32(((HiddenField)playerItem.FindControl("EspnPlayerRefHidden")).Value);
                thisEspnPlayer.Save();

                //Add new PlayerClub
                PlayerClub thisPlayerClub = new PlayerClub();
                thisPlayerClub.AddNew();
                thisPlayerClub.PlayerId = thisPlayer.Id;
                thisPlayerClub.ClubId = clubId;
                thisPlayerClub.StartDate = createDate;
                thisPlayerClub.EndDate = new DateTime(9999, 12, 31);
                thisPlayerClub.Save();
            }
            else
            {
                //Expire current player club record
                PlayerClub thisPlayerClub = new PlayerClub();
                thisPlayerClub.Where.PlayerId.Value = ((HiddenField)playerItem.FindControl("ExistingPlayerIdHidden")).Value;
                thisPlayerClub.Where.StartDate.Value = createDate;
                thisPlayerClub.Where.StartDate.Operator = MyGeneration.dOOdads.WhereParameter.Operand.LessThanOrEqual;
                thisPlayerClub.Where.EndDate.Value = createDate;
                thisPlayerClub.Where.EndDate.Operator = MyGeneration.dOOdads.WhereParameter.Operand.GreaterThanOrEqual;

                if (thisPlayerClub.Query.Load())
                {
                    thisPlayerClub.EndDate = createDate;
                    thisPlayerClub.Save();
                }

                thisPlayerClub.AddNew();
                thisPlayerClub.PlayerId = Convert.ToInt32(((HiddenField)playerItem.FindControl("ExistingPlayerIdHidden")).Value);
                thisPlayerClub.ClubId = clubId;
                thisPlayerClub.StartDate = createDate;
                thisPlayerClub.EndDate = new DateTime(9999, 12, 31);
                thisPlayerClub.Save();
            }
            
            //Delete player from Transfer table
            Transfer thisTransfer = new Transfer();
            thisTransfer.Where.Id.Value = transferId;
            if (thisTransfer.Query.Load())
            {
                thisTransfer.MarkAsDeleted();
                thisTransfer.Save();
                playerItem.Visible = false;
            }
        }
        PlayerRepeater.DataBind();
    }
}



